package com.bhcj.telling.utils;

import android.content.res.AssetManager;

import com.alibaba.fastjson.JSON;
import com.bhcj.telling.model.bean.GuaWords;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GuaUtil {

    private static Map<String, GuaWords> guaWordsMap = new ConcurrentHashMap<>();
    public static final char FRONT = '正';
    public static final char BACK = '反';
    public static final String ONE = "1";
    public static final String ZERO = "0";

    public static boolean isOne(String bo) {
        return ONE.equals(parse(bo));
    }

    public static boolean isOne(Character c) {
        return ONE.equals(String.valueOf(c));
    }

    public static String parse(String bo) {
        int count = 0;
        for (char character : bo.toCharArray()) {
            if (FRONT == character) {
                count++;
            }
        }
        if (count % 2 == 0) {
            return "1";
        }
        return "0";
    }

    public static GuaWords getGuaWords(AssetManager assetManager, String identity) {
        GuaWords guaWords = guaWordsMap.get(identity);
        if (null != guaWords) {
            return guaWords;
        }
        String jsonString = FileUtils.readJsonFile(assetManager, identity);
        GuaWords words = JSON.parseObject(jsonString, GuaWords.class);
        guaWordsMap.put(identity, words);
        return words;
    }

}
